//
//  MediaPlayerVC.m
//  LiteraryTune
//
//  Created by 陈雪丹 on 2018/6/2.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "MediaPlayerVC.h"
//#import <AVFoundation/AVFoundation.h>
//#import <AVKit/AVKit.h>
#import <ZFPlayer/ZFPlayer.h>
#import "ZFAVPlayerManager.h"
#import "KSMediaPlayerManager.h"
#import "ZFPlayerControlView.h"
#import "ZFIJKPlayerManager.h"
#import "UIView+ZFFrame.h"

@interface MediaPlayerVC ()
@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;


@end

@implementation MediaPlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
//    NSLog(@"1111111");
    
    
    
//    AVPlayer *avPlayer= [AVPlayer playerWithURL:[NSURL URLWithString:_urlString]];
//    // player的控制器对象
//    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
//    // 控制器的player播放器
//    playerViewController.player = avPlayer;
//    // 试图的填充模式
//    playerViewController.videoGravity = AVLayerVideoGravityResizeAspect;
//    // 是否显示播放控制条
//    playerViewController.showsPlaybackControls = YES;
//    // 设置显示的Frame
//    playerViewController.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
//    playerViewController.view.translatesAutoresizingMaskIntoConstraints = YES;
//    // 将播放器控制器添加到当前页面控制器中
//    [self addChildViewController:playerViewController];
//    // view一定要添加，否则将不显示
//    [self.view addSubview:playerViewController.view];
//    // 播放
//    [playerViewController.player play];
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.containerView];
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    self.player = [[ZFPlayerController alloc] initWithPlayerManager:playerManager containerView:self.containerView];
    self.player.controlView = self.controlView;
    playerManager.assetURL = [NSURL URLWithString:_urlString];
    [self.controlView showTitle:nil coverURLString:nil zhanweitu:self.imgString fullScreenMode:ZFFullScreenModeLandscape];
    
}


- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    CGFloat x = 0;
    CGFloat y = 0;//CGRectGetMaxY(self.navigationController.navigationBar.frame);
    CGFloat w = CGRectGetWidth(self.view.frame);
    CGFloat h = w*9/16;
    self.containerView.frame = CGRectMake(x, y, w, h);

    w = 200;
    h = 35;
    x = (self.containerView.width - w)/2;
    y = (self.containerView.height - h)/2;
    //    self.textField.frame = CGRectMake(x, y, w, h);

    w = 44;
    h = w;
    x = (CGRectGetWidth(self.containerView.frame)-w)/2;
    y = (CGRectGetHeight(self.containerView.frame)-h)/2;
//    self.playBtn.frame = CGRectMake(x, y, w, h);
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    if (self.player.isFullScreen) {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

- (BOOL)prefersStatusBarHidden {
    return self.player.isStatusBarHidden;
}

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation {
    return UIStatusBarAnimationSlide;
}

- (BOOL)shouldAutorotate {
    return self.player.shouldAutorotate;
}

//- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self.textField resignFirstResponder];
//}

#pragma mark - about keyboard orientation

/// 键盘支持横屏，这里必须设置支持多个方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if (self.player.isFullScreen) {
        return UIInterfaceOrientationMaskLandscape;
    }
    return UIInterfaceOrientationMaskPortrait;
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
    }
    return _controlView;
}


- (UIView *)containerView {
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = [UIColor orangeColor];
    }
    return _containerView;
}



@end
